/*
 * JBToolbar.java
 *
 * Created on October 10, 2007, 1:50 PM
 */

package org.easywrite.jbrowser;

import java.awt.Component;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import org.jdesktop.jdic.browser.WebBrowser;

/**
 *
 * @author  Perw_
 */
public class JBToolbar extends javax.swing.JPanel {
    
    /** Creates new form JBToolbar */
    public JBToolbar() {
           initComponents();      
                 
            InBrowsTopComponent.getDefault().addPropertyChangeListener( new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    Component[] comps = InBrowsTopComponent.getDefault().getComponents();
                    
                    if (comps.length > 0)
                    {
                        ((WebBrowser)comps[0]).setSize(InBrowsTopComponent.getDefault().getSize());
                    
                        ((WebBrowser)comps[0]).setVisible(true);
                    }
                }            
            }
            );
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jTextField1.setText(org.openide.util.NbBundle.getMessage(JBToolbar.class, "JBToolbar.jTextField1.text")); // NOI18N
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jButton1.setText(org.openide.util.NbBundle.getMessage(JBToolbar.class, "JBToolbar.jButton1.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 331, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
         int i = evt.getKeyChar();
         if (i==10){//The ENTER KEY
            // we display the google url.
            try{
		 
                Component[] comps = InBrowsTopComponent.getDefault().getComponents();
                
                InBrowsTopComponent.getDefault().open();

                WebBrowser browser1 = null;
                if (comps.length == 0)
                    {
                     browser1 = new WebBrowser(new URL("http://www.google.com"));
                    InBrowsTopComponent.getDefault().add(browser1);    
                    
                    InBrowsTopComponent.getDefault().getParent().addComponentListener(new ComponentAdapter()
                    {
                        public void componentResized(ComponentEvent e) 
                         {
                            int a = 1;
                            
                             Component[] comps = InBrowsTopComponent.getDefault().getComponents();

                            if ((comps!= null) && (comps.length > 0))
                            {
                                ((WebBrowser)comps[0]).setSize(InBrowsTopComponent.getDefault().getSize());

                                ((WebBrowser)comps[0]).setVisible(true);
                            }
                         }
                    });
                    browser1.addMouseListener( new MouseAdapter() {
                        public void mouseEntered(MouseEvent e) {
                            Component[] comps = InBrowsTopComponent.getDefault().getComponents();

                            if (comps.length > 0)
                            {
                                ((WebBrowser)comps[0]).setSize(InBrowsTopComponent.getDefault().getSize());

                                ((WebBrowser)comps[0]).setVisible(true);
                            }
                        }
                    });
                }
                else
                   browser1 = ((WebBrowser)comps[0]);
                
                browser1.setURL(new URL("http://"+jTextField1.getText()));
             
               browser1.setVisible(true);
               
               
                
            } catch (Exception eee){
                return;//nothing much to do
            }
        }
    }//GEN-LAST:event_jTextField1KeyTyped
        public void search(String keywd)
    {
        jTextField1.setText(keywd);
        
        try{          
              
                Component[] comps = InBrowsTopComponent.getDefault().getComponents();
                
                InBrowsTopComponent.getDefault().open();                
                
                
                WebBrowser browser1 = null;
                if (comps.length == 0)
                {
                     browser1 = new WebBrowser(new URL("http://www.google.com"));
                    InBrowsTopComponent.getDefault().add(browser1);    
                    
                    InBrowsTopComponent.getDefault().getParent().addComponentListener(new ComponentAdapter()
                    {
                        public void componentResized(ComponentEvent e) 
                         {
                            int a = 1;
                            
                             Component[] comps = InBrowsTopComponent.getDefault().getComponents();

                            if ((comps!= null) && (comps.length > 0))
                            {
                                ((WebBrowser)comps[0]).setSize(InBrowsTopComponent.getDefault().getSize());

                                ((WebBrowser)comps[0]).setVisible(true);
                            }
                         }
                    });
                    browser1.addMouseListener( new MouseAdapter() {
                        public void mouseEntered(MouseEvent e) {
                            Component[] comps = InBrowsTopComponent.getDefault().getComponents();

                            if (comps.length > 0)
                            {
                                ((WebBrowser)comps[0]).setSize(InBrowsTopComponent.getDefault().getSize());

                                ((WebBrowser)comps[0]).setVisible(true);
                            }
                        }
                    });
                }
                else
                   browser1 = ((WebBrowser)comps[0]);
                
                browser1.setURL(new URL("http://"+jTextField1.getText()));            
               
               browser1.setVisible(true);              
               
                
            } catch (Exception eee){
                return;//nothing much to do
            }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
    
}
